//
//  XNApprovePopView.m
//  XINNingOA
//
//  Created by samuel on 2021/7/15.
//

#import "XNApprovePopView.h"
#import "XNHud.h"
#import "XNCarInfoModel.h"

@interface XNApprovePopView()<UIPickerViewDelegate,UIPickerViewDataSource,UITextViewDelegate>

@property (nonatomic, weak)UIView *titleView;

@property (nonatomic, weak)UIView *backView;

@property (nonatomic, weak) UILabel *contentLab;

@property (nonatomic, weak) UIPickerView *picker;

@property (nonatomic, weak) UILabel *placeHolder;

@property (nonatomic, weak) UITextView *textView;

@property (nonatomic, copy) NSString *carStr;

@end

@implementation XNApprovePopView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self renderView];
        
    }
    return self;
}

- (void)renderView
{
    UIView *titleView = [[UIView alloc] init];
    titleView.backgroundColor = [UIColor colorWithHexString:@"#358AFF"];
    [self addSubview:titleView];
    self.titleView = titleView;
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(52);
    }];
    
    UILabel *title = [[UILabel alloc] init];
    title.text = @"审批意见";
    title.textColor = [UIColor whiteColor];
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [titleView addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(titleView);
        make.height.mas_equalTo(20);
    }];
    
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    [self addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView.mas_bottom);
        make.left.right.equalTo(self);
        make.bottom.equalTo(self);
    }];
    
    UIPickerView *picker = [[UIPickerView alloc] init];
    picker.delegate = self;
    picker.dataSource = self;
    [contentView addSubview:picker];
    self.picker = picker;
    [picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(8);
        make.right.equalTo(contentView).offset(-8);
        make.top.equalTo(contentView).offset(10);
        make.height.mas_equalTo(80);
    }];
    
    UITextView *textView = [[UITextView alloc] init];
    textView.layer.cornerRadius = 10;
    textView.layer.borderWidth = 1;
    textView.layer.borderColor = [UIColor colorWithHexString:@"#E0E0E0"].CGColor;
    textView.font = [UIFont systemFontOfSize:14];
    textView.clipsToBounds = YES;
    textView.delegate = self;
    [contentView addSubview:textView];
    self.textView = textView;
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(8);
        make.right.equalTo(contentView).offset(-8);
        make.top.equalTo(picker.mas_bottom).offset(10);
        make.height.mas_equalTo(150);
    }];
    
    UILabel *placeHolder = [[UILabel alloc] init];
    placeHolder.text = @"请输入审批意见";
    placeHolder.textColor = [UIColor lightGrayColor];
    placeHolder.numberOfLines = 0;
    placeHolder.font = [UIFont systemFontOfSize:14];
    placeHolder.contentMode = UIViewContentModeTop;
    [textView addSubview:placeHolder];
    self.placeHolder = placeHolder;
    [placeHolder mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(textView).offset(5);
        make.top.equalTo(textView).offset(8);
    }];
    
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"#E0E0E0"];
    [contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(1);
        make.top.equalTo(textView.mas_bottom).offset(10);
    }];
    
    UIButton *cancel = [[UIButton alloc] init];
    cancel.backgroundColor = [UIColor whiteColor];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cancel.titleLabel.textAlignment = NSTextAlignmentCenter;
    cancel.titleLabel.font = [UIFont systemFontOfSize:18];
    [cancel addTarget:self action:@selector(dismissPopView:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancel];
    [cancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self);
        make.right.equalTo(self.mas_centerX);
        make.top.equalTo(line.mas_bottom);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(contentView);
    }];
    
    
    UIButton *determine = [[UIButton alloc] init];
    determine.backgroundColor = [UIColor whiteColor];
    [determine setTitle:@"确定" forState:UIControlStateNormal];
    [determine setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    determine.titleLabel.textAlignment = NSTextAlignmentCenter;
    determine.titleLabel.font = [UIFont systemFontOfSize:18];
    [determine addTarget:self action:@selector(submitOrder:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:determine];
    [determine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self);
        make.left.equalTo(self.mas_centerX);
        make.top.equalTo(line.mas_bottom);
        make.height.mas_equalTo(50);
        make.bottom.equalTo(contentView);
    }];
    
    UILabel *bottomLine = [[UILabel alloc] init];
    bottomLine.backgroundColor = [UIColor colorWithHexString:@"#E0E0E0"];
    [self addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom);
        make.width.mas_equalTo(2);
        make.bottom.equalTo(determine);
        make.centerX.equalTo(self);
    }];
    
    

}

- (void)setDataArray:(NSArray *)dataArray
{
    NSMutableArray *array = [NSMutableArray array];
    XNCarInfoModel *infoModel = [[XNCarInfoModel alloc] init];
    infoModel.carNo = @"请选择车牌号";
    [array addObject:infoModel];
    [dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [array addObject:obj];
    }];
    
    _dataArray = [NSArray arrayWithArray:array];
    
    [self.picker reloadAllComponents];
    
}

- (void)setShowPicker:(BOOL)showPicker
{
    _showPicker = showPicker;
    if (showPicker) {
        [self.picker mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(150);
        }];
        [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }else{
        [self.picker mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(150);
        }];
    }
}

-(void)textViewDidChange:(UITextView *)textView{
    if ([self.textView.text isEqualToString:@""]){
        self.placeHolder.hidden = NO;
    }else{
        self.placeHolder.hidden = YES;
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArray.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 27;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    XNCarInfoModel *infoModel = self.dataArray[row];
    return infoModel.carNo;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (row == 0) {
        self.carStr = @"";
    }else{
        XNCarInfoModel *infoModel = self.dataArray[row];
        self.carStr = infoModel.carNo;
    }
}



- (void)dismissPopView:(UITapGestureRecognizer *)tap
{
    [self dismmisPop];
}


- (void)submitOrder:(UIButton *)sender
{
    NSString *checkStr = self.textView.text;
    if (self.showPicker) {
        if (self.carStr.length > 0) {
            checkStr = self.carStr;
        }else{
            [XNHud showToastWithText:@"请选择车辆"];
            return;
        }
    }else{
        if (checkStr.length == 0) {
            [XNHud showToastWithText:@"审批意见不能为空"];
            return;
        }
    }
    [self dismmisPop];
    if ([self.delegate respondsToSelector:@selector(PopView:checkMessage:ApproveType:)]) {
        [self.delegate PopView:self checkMessage:checkStr ApproveType:self.approveType];
    }

}


//出现
- (void)show {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *backView = [[UIView alloc] initWithFrame:keyWindow.bounds];
    self.backView = backView;
    self.backView.alpha = 0.3;
    self.backView.backgroundColor = [UIColor blackColor];
    [keyWindow addSubview:self.backView];
    [keyWindow addSubview:self];
    [keyWindow bringSubviewToFront:self];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.centerY.equalTo(backView);
    }];
    
    [keyWindow layoutIfNeeded];

}

//消失
- (void)dismmisPop {
    [self endEditing:YES];
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow layoutIfNeeded];
    [self.backView removeFromSuperview];
    [self removeFromSuperview];

}


@end
